package com.survey.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.survey.common.utils.DateUtils;
import com.survey.common.utils.SecurityUtils;
import com.survey.system.domain.SurIssue;
import com.survey.system.mapper.SurIssueMapper;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.survey.system.mapper.SurFeedbackMapper;
import com.survey.system.domain.SurFeedback;
import com.survey.system.service.ISurFeedbackService;

/**
 * 反馈信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-23
 */
@Service
public class SurFeedbackServiceImpl implements ISurFeedbackService 
{
    @Autowired
    private SurFeedbackMapper surFeedbackMapper;
    @Autowired
    private SurIssueMapper surIssueMapper;

    /**
     * 查询反馈信息
     * 
     * @param feedbackId 反馈信息主键
     * @return 反馈信息
     */
    @Override
    public SurFeedback selectSurFeedbackByFeedbackId(Long feedbackId)
    {
        return surFeedbackMapper.selectSurFeedbackByFeedbackId(feedbackId);
    }

    /**
     * 查询反馈信息列表
     * 
     * @param surFeedback 反馈信息
     * @return 反馈信息
     */
    @Override
    public List<SurFeedback> selectSurFeedbackList(SurFeedback surFeedback)
    {
        return surFeedbackMapper.selectSurFeedbackList(surFeedback);
    }

    /**
     * 新增反馈信息
     * 
     * @param surFeedback 反馈信息
     * @return 结果
     */
    @Override
    public int insertSurFeedback(SurFeedback surFeedback)
    {
        surFeedback.setCreateTime(DateUtils.getNowDate());
        surFeedback.setUserId(SecurityUtils.getUserId());
        SurIssue surIssue = new SurIssue();
        surIssue.setIssueId(surFeedback.getIssueId());
        surIssue.setIssueStatus(1L);
        surIssueMapper.updateSurIssue(surIssue);
        return surFeedbackMapper.insertSurFeedback(surFeedback);
    }

    /**
     * 修改反馈信息
     * 
     * @param surFeedback 反馈信息
     * @return 结果
     */
    @Override
    public int updateSurFeedback(SurFeedback surFeedback)
    {
        surFeedback.setUpdateTime(DateUtils.getNowDate());
        return surFeedbackMapper.updateSurFeedback(surFeedback);
    }

    /**
     * 批量删除反馈信息
     * 
     * @param feedbackIds 需要删除的反馈信息主键
     * @return 结果
     */
    @Override
    public int deleteSurFeedbackByFeedbackIds(Long[] feedbackIds)
    {
        return surFeedbackMapper.deleteSurFeedbackByFeedbackIds(feedbackIds);
    }

    /**
     * 删除反馈信息信息
     * 
     * @param feedbackId 反馈信息主键
     * @return 结果
     */
    @Override
    public int deleteSurFeedbackByFeedbackId(Long feedbackId)
    {
        return surFeedbackMapper.deleteSurFeedbackByFeedbackId(feedbackId);
    }

    @Override
    public List<Integer> countByDate() {
        List<Integer> numList = new ArrayList<>();
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        // 获取今天的数据（今天的日期格式化为字符串）
        String today = dateFormat.format(date);
        int countToday = surFeedbackMapper.countByDate(today);
        numList.add(countToday);  // 添加今天的数据

        // 获取过去6天的数据
        for (int i = 1; i <= 6; i++) {
            calendar.add(Calendar.DAY_OF_MONTH, -1);  // 每次回退1天
            Date time = calendar.getTime();
            String formattedDate = dateFormat.format(time);
            int count = surFeedbackMapper.countByDate(formattedDate);
            numList.add(count);
        }

        return numList;
    }

    @Override
    public Integer selectTodaySurFeedback() {
        return surFeedbackMapper.selectTodaySurFeedback();
    }
}
